package cn.lumora.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.lumora.core.security.rea.RSAKeyStorageService;
import cn.lumora.core.security.token.TempTokenService;
import cn.lumora.pojo.*;
import cn.lumora.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * 角色初始化服务实现类
 */
@Service
@Slf4j
public class CharacterInitServiceImpl implements CharacterInitService {


    /**
     * 角色背包服务
     */
    private final CharacterInventoryService characterInventoryService;

    /**
     * 天赋服务
     */
    private final TalentService talentService;

    /**
     * 角色天赋服务
     */
    private final CharacterTalentService characterTalentService;

    /**
     * 角色修炼状态表服务
     */
    private final CharacterCultivationService characterCultivationService;

    /**
     * 角色境界服务
     */
    private final CultivationStageService cultivationStageService;

    private final CultivationStageRankService cultivationStageRankService;

    public CharacterInitServiceImpl(CharacterInventoryService characterInventoryService, TalentService talentService, CharacterTalentService characterTalentService, CharacterCultivationService characterCultivationService, CultivationStageService cultivationStageService, CultivationStageRankService cultivationStageRankService) {
        this.characterInventoryService = characterInventoryService;
        this.talentService = talentService;
        this.characterTalentService = characterTalentService;
        this.characterCultivationService = characterCultivationService;
        this.cultivationStageService = cultivationStageService;
        this.cultivationStageRankService = cultivationStageRankService;
    }

    /**
     * 初始化天赋
     * @param characterId 角色id
     */
    @Override
    public String initCharacterTalent(String characterId) {

        try {
            log.info("初始化天赋");
            // 获取所有天赋
            LambdaQueryWrapper<Talent> talentLambdaQueryWrapper = new LambdaQueryWrapper<>();
            talentLambdaQueryWrapper.select(Talent::getId,Talent::getName);
            List<Talent> talentList = talentService.list(talentLambdaQueryWrapper);
            // 随机获取1到10个天赋
            int count = (int) (Math.random() * 10 + 1);
            log.info("随机获取{}个天赋", count);

            // 获取天赋对象列表
            List<CharacterTalent> characterTalents = new ArrayList<>();
            for (int i = 0; i < count; i++) {
                // 随机获取一个天赋
                Talent talent = talentList.get((int) (Math.random() * talentList.size()));
                // 创建天赋对象
                CharacterTalent characterTalent = CharacterTalent.builder()
                        .characterId(characterId)
                        .talentId(talent.getId())
                        .talentLevel(1)
                        .build();
                characterTalent.setCreateTime(LocalDateTime.now());
                characterTalent.setUpdateTime(LocalDateTime.now());
                characterTalents.add(characterTalent);
            }
            characterTalentService.saveBatch(characterTalents);

            log.info("初始化天赋成功");
            return null;
        } catch (Exception e) {
            log.error("初始化天赋失败", e);
            throw new RuntimeException("初始化天赋失败");
        }
    }


    /**
     * 初始化境界
     * @param characterId 角色id
     */
    @Override
    public void initCultivationStage(String characterId) {
        try {
            LambdaQueryWrapper<CultivationStage> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.select(CultivationStage::getId, CultivationStage::getRequiredExp);
            queryWrapper.orderByAsc(CultivationStage::getRequiredExp);
            queryWrapper.last("limit 1");
            CultivationStage cultivationStage = cultivationStageService.getOne(queryWrapper);

            // 境界id
            String cultivationStageId = cultivationStage.getId();
            LambdaQueryWrapper<CultivationStageRank> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.select(CultivationStageRank::getId, CultivationStageRank::getRequiredExp);
            queryWrapper1.eq(CultivationStageRank::getStageId, cultivationStageId);
            queryWrapper1.orderByAsc(CultivationStageRank::getRank);
            queryWrapper1.last("limit 1");
            CultivationStageRank cultivationStageRank = cultivationStageRankService.getOne(queryWrapper1);
            if (ObjectUtil.isNotEmpty(cultivationStageRank)){
                CharacterCultivation characterCultivation = CharacterCultivation.builder()
                        .characterId(characterId)
                        .currentStageId(cultivationStageRank.getId())
                        .currentExp(0L)
                        .build();
                characterCultivationService.save(characterCultivation);
            }

            log.info("初始化境界成功");

        }catch (Exception e){
            log.error("初始化境界失败",e);
            throw new RuntimeException("初始化境界失败");
        }
    }

    /**
     * 初始化背包
     *
     * @param characterId 角色id
     */
    @Override
    public String initCharacterInventory(String characterId) {
        try {
            CharacterInventory characterInventory = CharacterInventory.builder()
                    .characterId(characterId)
                    .build();
            characterInventory.setCreateTime(LocalDateTime.now());
            characterInventory.setUpdateTime(LocalDateTime.now());
            characterInventoryService.save(characterInventory);
            log.info("初始化背包成功");
            return characterInventory.getId();
        } catch (Exception e) {
            log.error("初始化背包失败", e);
            throw new RuntimeException("初始化背包失败");
        }
    }
}
